package com.atguigu.lease.web.admin.custom.webMvcConfig;


import com.atguigu.lease.web.admin.custom.converter.ConverterFactorys;
import com.atguigu.lease.web.admin.interceptors.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class webConfig implements WebMvcConfigurer {

    @Value("${web.admin.interceptor-include-path}")
    private String [] inCludePath;
    @Value("${web.admin.interceptor-exclude-path}")
    private  String [] excludePath;
    //增加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns(inCludePath)
                //放行地址:  不需要登录即可访问的地址
                .excludePathPatterns(excludePath) //排除登录界面
                .excludePathPatterns(excludePath) //排除验证码
                .order(1);
    }

    //增加类型转换器
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new ConverterFactorys());
        //registry.addConverter(new ConverterApartmentById());

    }
    //配置跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}
